package com.xilihui.xlh.core.util;

import android.app.Activity;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.xilihui.xlh.business.dialogs.ShareDialog;

/**
 * 2017/7/25.
 */

public class ShareUtil {

    public static void shareBoard(final Activity activity, final UMWeb web, Platform platform) {
        ShareDialog dialog = new ShareDialog(activity);

        if (platform == Platform.ALL) {
            dialog.setWechatVisible(true);
            dialog.setWechatCircleVisible(true);
            dialog.setQQVisible(true);
            dialog.setQZoneVisible(true);
        } else if (platform == Platform.QQ) {
            dialog.setWechatVisible(false);
            dialog.setWechatCircleVisible(false);
            dialog.setQQVisible(true);
            dialog.setQZoneVisible(true);
        } else if (platform == Platform.WECHAT) {
            dialog.setWechatVisible(true);
            dialog.setWechatCircleVisible(true);
            dialog.setQQVisible(false);
            dialog.setQZoneVisible(false);
        }

        dialog.setCallBack(new ShareDialog.DialogShareCallBack() {
            @Override
            public void onSelected(int platform) {
                switch (platform) {
                    case ShareDialog.WEIXIN:
                        new ShareAction(activity)
                                .withMedia(web)
                                .setPlatform(SHARE_MEDIA.WEIXIN)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                    case ShareDialog.WEIXIN_CIRCLE:
                        new ShareAction(activity)
                                .withMedia(web)
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                    case ShareDialog.QQ:
                        new ShareAction(activity)
                                .withMedia(web)
                                .setPlatform(SHARE_MEDIA.QQ)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                    case ShareDialog.QZONE:
                        new ShareAction(activity)
                                .withMedia(web)
                                .setPlatform(SHARE_MEDIA.QZONE)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                }
            }
        });
        dialog.show();
    }

    public static void shareImage(final Activity activity, final UMImage image, Platform platform) {
        ShareDialog dialog = new ShareDialog(activity);

        if (platform == Platform.ALL) {
            dialog.setWechatVisible(true);
            dialog.setWechatCircleVisible(true);
            dialog.setQQVisible(true);
            dialog.setQZoneVisible(true);
        } else if (platform == Platform.QQ) {
            dialog.setWechatVisible(false);
            dialog.setWechatCircleVisible(false);
            dialog.setQQVisible(true);
            dialog.setQZoneVisible(true);
        } else if (platform == Platform.WECHAT) {
            dialog.setWechatVisible(true);
            dialog.setWechatCircleVisible(true);
            dialog.setQQVisible(false);
            dialog.setQZoneVisible(false);
        }

        dialog.setCallBack(new ShareDialog.DialogShareCallBack() {
            @Override
            public void onSelected(int platform) {
                switch (platform) {
                    case ShareDialog.WEIXIN:
                        new ShareAction(activity)
                                .withMedia(image)
                                .setPlatform(SHARE_MEDIA.WEIXIN)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                    case ShareDialog.WEIXIN_CIRCLE:
                        new ShareAction(activity)
                                .withMedia(image)
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                    case ShareDialog.QQ:
                        new ShareAction(activity)
                                .withMedia(image)
                                .setPlatform(SHARE_MEDIA.QQ)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                    case ShareDialog.QZONE:
                        new ShareAction(activity)
                                .withMedia(image)
                                .setPlatform(SHARE_MEDIA.QZONE)
                                .setCallback(new ShareListener())
                                .share();
                        break;
                }
            }
        });
        dialog.show();
    }

    private static class ShareListener implements UMShareListener {

        @Override
        public void onStart(SHARE_MEDIA share_media) {
            LogUtil.i("mylog","share start");
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            ToastUtil.toast("分享成功");
            LogUtil.i("mylog","share success");            
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            ToastUtil.toast(throwable.getMessage());
            LogUtil.i("mylog","share error");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            ToastUtil.toast("分享已取消");
            LogUtil.i("mylog","share cancel");
        }
    }

    public enum Platform {
        QQ, WECHAT, ALL
    }
}